<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 *
 * PARAMS:
 *    assign      - assign to variable - otherwirse to 'articles'
 *    type        - coma separated list of types to be in articles
 *    category    - select articles from given categories
 *    rcategory   - select articles from given categories and thair childs
 *    limit       - limit articles
 *    order       - sort articles by
 *    filter      - string of filters  field=value;field2<value;field3>value
 *    assignpages - assign pages array to variable
 */


function smarty_function_meccano_articles($params, &$smarty)
{
   $ARTICLES = new articles();
   $KERNEL   = &$GLOBALS['KERNEL'];
   $FILTERS  = new filters();


   foreach($params AS $k=>$value)
   {
      $$k = $value;
   }
   $set['field']['active']['type'] = 'exact';
   $set['field']['active']['value'] = '1';


   # FILTER ARTICLES BY TYPE
   if(isset($type))
   {
      $not = 'have';
      if($type[0] == '!')
      {
         $type = substr($type, 1);
         $not = 'nothave';
      }
      $type = explode(',', $type);
      $article_types = $KERNEL->getTypes('a');
      array_walk($type, 'trim');

      if(count($type) > 0)
      {
         $tmp = array();
         foreach($type AS $t)
         {
            if(($t = array_search($t, $article_types)) !== FALSE) {$tmp[] = $t; }
         }
         if(count($tmp) > 0)
         {
            $set['field']['types']['type']  = $not;
            $set['field']['types']['value'] = count($tmp) == 1 ? reset($tmp) : $tmp;
         }
      }
   }

   if(!empty($category))
   {
      $not = 'exact';
      if($category[0] == '!')
      {
         $category = substr($category, 1);
         $not = 'not';
      }
      $category = explode(',', $category);
      $category = $FILTERS->filterForInteger($category);
      if(count($category) > 0)
      {
         $set['field']['cat']['type']  = $not;
         $set['field']['cat']['value'] = count($category) == 1 ? reset($category) : $category;
      }
   }

   if(isset($rcategory))
   {
      $not = 'exact';
      if($rcategory[0] == '!')
      {
         $rcategory = substr($rcategory, 1);
         $not = 'not';
      }
      $rcategory = explode(',', $rcategory);
      $rcategory = $FILTERS->filterForInteger($rcategory);
      $tmp = array();
      for($i=0, $t=count($rcategory); $i<$t; $i++)
      {
         $tmp[] = $rcategory[$i];
         $tmp2 = tree_static::getChildrenId($rcategory[$i]);
         if(is_array($tmp2)) $tmp = array_merge($tmp, $tmp2);
      }
      $rcategory = $tmp;
      unset($tmp, $tmp2);

      if(count($rcategory) > 0)
      {
         $set['field']['cat']['type']  = 'exact';
         $set['field']['cat']['value'] = count($rcategory) == 1 ? reset($rcategory) : $rcategory;
      }
   }


   /**
    * FILTER FORMAT: views>10;sdate=2009-06-28,2009-06-29
    */
   if(!empty($filter))
   {
      $filter = explode(';', $filter);
      foreach($filter AS $f)
      {
         preg_match_all('/([\w\d_]+)(!|=|<|>)([\w\d\s,-]+)/', $f, $matches, PREG_SET_ORDER);
         if(is_array($matches))
         {
            $condition = reset($matches);

            if(is_array($condition))
            {
               $value = explode(',', $condition[3]);
               if(count($value) < 2) $value = $condition[3];
               $set['field'][$condition[1]]['value'] = $value;

               switch($condition[2])
               {
                  case '<':
                     $set['field'][$condition[1]]['type'] = 'less';
                     break;
                  case '>':
                     $set['field'][$condition[1]]['type'] = 'more';
                     break;
                  case '!':
                     $set['field'][$condition[1]]['type'] = 'not';
                     break;
                  case '=':
                  default:
                     $set['field'][$condition[1]]['type'] = 'exact';
               }
            }
         }
      }
   }


   # SET CURRENT PAGE
   if(isset($page) && $FILTERS->isValidInteger($page))
      paginator::setCurrentPage($page);
   if(isset($limit) && $limit == 1)
      paginator::setCurrentPage (1);


   $ARTICLES->setFilter($set);

   if(isset($limit))
   {
      paginator::setItemsOnPage ($limit);
   }


   # ORDERING ARTICLES
   if(isset($order) || isset($random))
   {
      if(isset($random))
         $order = 'random';
      elseif(preg_match('/rand\(\)/i', $order))
         $order = 'random';

      $ARTICLES->setOrder($order);
   }


   $articles = $ARTICLES->getArticles();
   if(isset($limit) && $limit == 1 && is_array($articles) && count($articles) == 1) $articles = reset($articles);
   if(!isset($assign)) $assign = 'articles';
   if(isset($assignpages))
   {
      $smarty->assign($assignpages, paginator::getPages());
   }

   $smarty->assign($assign, $articles);
}